# main_producer.py
from kafka_producer import KafkaProducerWrapper
import requests
import json


if __name__ == "__main__":
    # 初始化
    producer = KafkaProducerWrapper()

    while True:
        response = requests.get(
            "https://api.open-meteo.com/v1/forecast",
            params={
                "latitude": 51.5,
                "longitude": -0.11,
                "current": "temperature_2m"
            },
        )
        data = response.json()  # 解析 JSON 响应
        # print(data)
        # print(json.dumps(data))
        # 发送一条消息
        producer.send(json.dumps(data)) # 转为字符串后发送

        # 批量发送
        # messages = [f"message {i}" for i in range(10)]
        # producer.send_batch(messages)

        # 并发发送（提高效率）
        # producer.send_async(messages, thread_count=3)
